package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComBExlVO;
import com.jinzhi.jzweb.pojo.VtatisticsVo;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.strategy.factory.SourConfig;
import com.jinzhi.jzweb.utils.DoubleUtil;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * A赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComBSour")
@Api(value = "教师组竞赛平台", tags = {"A赛场查看分数统计"})
public class ExaminationAnswerSheetComBSourController extends BaseController {

    @Autowired
    private StuSignUpComService stuSignUpService;

    @Autowired
    private ExaminationResultsComService examinationResultsService;
    /**
     * A赛场成绩统计导出
     *
     * @param response
     */
    @ApiOperation(value = "竞赛平台-A赛场分数统计", notes = "A赛场成绩导出")
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:stuSignUpCom:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        List<ExaminationAnswerSheetComBExlVO> list1 = new LinkedList<>();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDT.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> list = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.eq("decrypt",1).
                        orderBy("id", false));
        for(StuSignUpComDO stuSignUpComDO:list.getRecords()){
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",stuSignUpComDO.getId());
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = examinationResultsComDO;
                ExaminationAnswerSheetComBExlVO exlVO=new ExaminationAnswerSheetComBExlVO();
                exlVO.setCandidateNumber(stuSignUpComDO.getCandidateNumber());
                exlVO.setName(stuSignUpComDO.getName());
                exlVO.setUnitName(stuSignUpComDO.getUnitName());
                exlVO.setIdNumber(stuSignUpComDO.getIdNumber());
                exlVO.setCandidateNumber(stuSignUpComDO.getNum2());
               // exlVO.setTicketNumber(stuSignUpComDO.getTicketNumber());
                // Double sour=obj.getTotalScore();
                if(obj.getTotalScore() == null){
                    obj.setTotalScore(0.0);
                }else{
                    BigDecimal jkk = new BigDecimal(obj.getTotalScore() * 0.3);
                    Double jkkk = jkk.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    obj.setTotalScore(jkkk);
                }
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }
                if(obj.getScoingb4()==null){
                    obj.setScoingb4(0.0);
                }
                if(obj.getScoingb5()==null){
                    obj.setScoingb5(0.0);
                }
                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }
                if(obj.getScoingb4s()==null){
                    obj.setScoingb4s(0.0);
                }
                if(obj.getScoingb5s()==null){
                    obj.setScoingb5s(0.0);
                }

                double s11=(obj.getScoingb1());
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s=(obj.getScoingb1s());
                double f11s = DoubleUtil.douToTwo(s11s);
                String a=String.valueOf(f11)+"";

                double s12=(obj.getScoingb2());
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s=(obj.getScoingb2s());
                double f12s = DoubleUtil.douToTwo(s12s);
                String b=String.valueOf(f12)+"";


                double s13=(obj.getScoingb3());
                double f13 = DoubleUtil.douToTwo(s13);


                double s13s=(obj.getScoingb3s());
                double f13s = DoubleUtil.douToTwo(s13s);
                String c=String.valueOf(f13)+"";

                double s14=(obj.getScoingb4());
                double f14 = DoubleUtil.douToTwo(s14);


                double s14s=(obj.getScoingb4s());
                double f14s = DoubleUtil.douToTwo(s14s);
                String d=String.valueOf(f14)+"";

                double s15=(obj.getScoingb5());
                double f15 = DoubleUtil.douToTwo(s15);


                double s15s=(obj.getScoingb5s());
                double f15s = DoubleUtil.douToTwo(s15s);
                String e=String.valueOf(f15)+"";

                exlVO.setScore1(a);
                exlVO.setScore2(b);
                exlVO.setScore3(c);

                exlVO.setJkcj(String.valueOf(obj.getTotalScore()));

                double s1= Double.parseDouble((a));
                double s2= Double.parseDouble((b));
                double s3= Double.parseDouble((c));
                double jg = (s1 + s2 +s3);
                jg = (jg / 3);
                jg = jg +  obj.getTotalScore();
                BigDecimal bg = new BigDecimal(jg);

                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                exlVO.setScore(String.valueOf(f1));
                exlVO.setRemark(examinationResultsComDO.getRemarkA());
                list1.add(exlVO);
            }else {
                ExaminationAnswerSheetComBExlVO obj = new ExaminationAnswerSheetComBExlVO();
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setCandidateNumber(stuSignUpComDO.getNum2());
//                obj.setScoingb1(0.0);
//                obj.setScoingb2(0.0);
//                obj.setScoingb3(0.0);
                obj.setJkcj("0.0");
                obj.setScore("0.0");
                obj.setScore1("0.0");
                obj.setScore2("0.0");
                obj.setScore3("0.0");
                list1.add(obj);

            }
            // list1.add(stuSignUpComVo);
            list1 = list1.stream().sorted(Comparator.comparing(ExaminationAnswerSheetComBExlVO::getCandidateNumber,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());
        }

        //导出操作
        EasyPoiUtil.exportExcel(list1, "A赛场成绩导出", "A赛场成绩导出", ExaminationAnswerSheetComBExlVO.class, "A赛场成绩导出", response);
    }


    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComBSour";
    }



    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping(value = "/rubrinfo")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:rubrinfo")
    String rubrinfo( Model model) {
        return "jzweb/examinationAnswerSheetCom/rubrinfo";
    }

    @GetMapping(value = "/exitScore")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:exitScore")
    String exitScore(Model model) {
        return "jzweb/examinationAnswerSheetCom/exitScore";
    }


    /**
     * B赛场理论成绩统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<VtatisticsVo> list(ExaminationResultsComDO examinationAnswerSheetComDTO){
        VtatisticsVo vtatisticsVo = new VtatisticsVo();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDTO.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> page = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),

                wrapper.orderBy("id", false));

        double f1 = 0.0; //个人总分
        double max = 0.0; //最高分
        double min = 100.0; //最低分
        List<ExaminationResultsComDO> list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",page.getRecords().get(i).getId());
            StuSignUpComDO stuSignUpComDO=page.getRecords().get(i);
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = examinationResultsComDO;
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setCandidateNumber(stuSignUpComDO.getNum2());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                System.out.println(obj.getTotalScore());
               // Double sour=obj.getTotalScore();
                if(obj.getTotalScore() == null){
                    obj.setTotalScore(0.0);
                }else{
                    BigDecimal jkk = new BigDecimal(obj.getTotalScore() * 0.3);
                    Double jkkk = jkk.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    obj.setTotalScore(jkkk);
                }
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }
                if(obj.getScoingb4()==null){
                    obj.setScoingb4(0.0);
                }
                if(obj.getScoingb5()==null){
                    obj.setScoingb5(0.0);
                }
                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }
                if(obj.getScoingb4s()==null){
                    obj.setScoingb4s(0.0);
                }
                if(obj.getScoingb5s()==null){
                    obj.setScoingb5s(0.0);
                }

                double s11=(obj.getScoingb1());
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s=(obj.getScoingb1s());
                double f11s = DoubleUtil.douToTwo(s11s);
                String a=String.valueOf(f11)+"";

                double s12=(obj.getScoingb2());
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s=(obj.getScoingb2s());
                double f12s = DoubleUtil.douToTwo(s12s);
                String b=String.valueOf(f12)+"";


                double s13=(obj.getScoingb3());
                double f13 = DoubleUtil.douToTwo(s13);


                double s13s=(obj.getScoingb3s());
                double f13s = DoubleUtil.douToTwo(s13s);
                String c=String.valueOf(f13)+"";

                double s14=(obj.getScoingb4());
                double f14 = DoubleUtil.douToTwo(s14);


                double s14s=(obj.getScoingb4s());
                double f14s = DoubleUtil.douToTwo(s14s);
                String d=String.valueOf(f14)+"";

                double s15=(obj.getScoingb5());
                double f15 = DoubleUtil.douToTwo(s15);


                double s15s=(obj.getScoingb5s());
                double f15s = DoubleUtil.douToTwo(s15s);
                String e=String.valueOf(f15)+"";

                obj.setScoreA(a);
                obj.setScoreB(b);
                obj.setScoreC(c);

                double s1= Double.parseDouble((a));
                double s2= Double.parseDouble((b));
                double s3= Double.parseDouble((c));
                double jg = (s1 + s2 +s3);
                jg = (jg / 3);
                jg = jg + obj.getTotalScore();
                BigDecimal bg = new BigDecimal(jg);
                f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                obj.setScore(f1);
                obj.setRemarkA(examinationResultsComDO.getRemarkA());
                list.add(obj);
            }else {
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setCandidateNumber(stuSignUpComDO.getNum2());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                obj.setTotalScore(0.0);
                obj.setScoingb1(0.0);
                obj.setScoingb2(0.0);
                obj.setScoingb3(0.0);
                obj.setScoingb4(0.0);
                obj.setScoingb5(0.0);
                obj.setScoingb1s(0.0);
                obj.setScoingb2s(0.0);
                obj.setScoingb3s(0.0);
                obj.setScoingb4s(0.0);
                obj.setScoingb5s(0.0);
                obj.setScore(0.0);
                obj.setScoreA("0.0");
                obj.setScoreB("0.0");
                obj.setScoreC("0.0");
                list.add(obj);
            }
            //获取最高分
            if(f1>=max){
                max = f1;
                vtatisticsVo.setMaxScore(max);
            }
            //获取最低分
            if(f1 <= min){
                min = f1;
                vtatisticsVo.setMinScore(min);
            }
        }
        List list1 = list.stream().sorted(Comparator.comparing(ExaminationResultsComDO::getCandidateNumber,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());
        page.setRecords(list1);
        //获取竞赛人数
        vtatisticsVo.setNumberPeople(page.getRecords().size());
        vtatisticsVo.setStuSignUpComDOs(page);
        return Result.ok(vtatisticsVo);
    }

}
